

jQuery(function ($) {
	//匹配手机号||邮箱号
	var reg = /^1[3456789]\d{9}$/;
	var reg_email = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
	var user = JSON.parse(localStorage.getItem('user_info')) || {};
	var token = user.token;
	//禁止输入中文
	// var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g
	// $('#tel').on('input', function () {
	// 	var value = $(this).val();
	// 	var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
	// 	this.value = value.replace(re, '');
	// });
	//判断格式
	$("#tel").blur(function () {
		// var reg = /^1[3456789]\d{9}$/;
		// var reg_email = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
		if (reg.test($("#tel").val()) || reg_email.test($("#tel").val())) {
			$(".tel-msg").text('');
			$('#input_code').removeClass('hide');
			change_pic();
		}
		// else {
		// 	$(".tel-msg").text('请输入正确的手机号或邮箱地址');
		// 	$('#input_code').addClass('hide');
		// 	$('#text').val('');
		// 	return;
		// }
		if ($("#tel").val() == '') {
			$(".tel-msg").text('对不起，账号不能为空');
			$('#input_code').addClass('hide');
			$('#text').val('');
			return;
		}
	});
	//密码不能少于 六位数
	$("#code").blur(function () {
		var password = $.trim($("#code").val());
		if (password.length == 0) {
			$(".code-msg").html("请输入密码");
			return;
		} else {
			$(".code-msg").html("");
		}
	});
	//登录
	$("#btn").click(function () {
		var loginCode = $.trim($("#tel").val());
		var password = $.trim($("#code").val());
		// var textCode = $.trim($('#text').val());
		var data = {
			"loginCode": loginCode,
			"password": password
			//非空验证
		};
		if (loginCode == "" || loginCode == null) {
			$("#tel").focus();
			$(".tel-msg").html("请输入正确的手机号或邮箱地址");
			return;
		} else if (password == "" || password == null) {
			$("#code").focus();
			$(".code-msg").html("对不起，登录密码不能为空");
			return;
		}
		if (reg.test($("#tel").val())) {
			login_ajax(1, succCallback);
		} else if (reg_email.test($("#tel").val())) {
			login_ajax(2, succCallback);
		} else {
			login_ajax(0, succCallback);
		}
		//成功后的回调函数
		function succCallback(data) {
			var id = data.data.id;
			var uid = data.data.uid;
			var user_name = data.data.loginName;
			var point = data.data.point;
			var levelId = data.data.levelId;
			var balance = data.data.balance;
			var growth = data.data.growth;
			var data = {
				"id": id,
				"uid": uid,
				"user_name": user_name,
				"point": point,
				"token": data.attributes.token,
				"level_id": levelId,
				"balance": balance,
				"growth": growth
			};
			localStorage.setItem("user_info", JSON.stringify(data));
			var isStoreGoods = JSON.parse(localStorage.getItem('noUsers')) || {};
			if (isStoreGoods.length) {
				var goodsIdArr = [];
				for (var i = 0; i < isStoreGoods.length; i++) {
					goodsIdArr.push(isStoreGoods[i].id);
				};
				var data = {
					"activity_id": 0,
					'user_id': id,
					'goods_ids': goodsIdArr,
					type: 1
				};
				send_ajax('post', '/api/cart/add', data, function callback(result) {});

				var prevLink = document.referrer;
				var url = window.location.host;
				if ($.trim(prevLink) == '') {
					location.href = '/index.html';
				} else {
					if (prevLink.indexOf(url) == -1) {
						//来自其它站点
						location.href = '/';
						return;
					}
					if (prevLink.indexOf('/login') != -1) {
						//来自注册页面
						location.href = '/';
						return;
					}
					location.href = prevLink;
				}
			} else {
				var prevLink = document.referrer;
				var url = window.location.host;
				if ($.trim(prevLink) == '') {
					location.href = '/index.html';
				} else {
					if (prevLink.indexOf(url) == -1) {
						//来自其它站点
						location.href = '/';
						return;
					}
					if (prevLink.indexOf('/login') != -1) {
						//来自注册页面
						location.href = '/';
						return;
					}
					location.href = prevLink;
				}
			}
		}
		//请求函数	
		function login_ajax(n, succCallback) {
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/user/login',
				data: JSON.stringify({ account: loginCode, password: password, type: n }),
				contentType: "application/json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				error: function error(_error) {
					//请求失败的回调方法 
					change_pic();
					alert('网络加载失败，请稍候再试!');
				},
				success: function success(data) {
					if (data.code == 200) {
						if (succCallback) {
							succCallback(data);
						}
					} else {
						$(".tel-msg").html(data.msg);
						change_pic();
					}
				}
			});
		}
	});
	var time = 60;
	var flag = true;
	var timer;
	//手机短信
	$('.getcode').click(function () {
		var reg = /^1[3456789]\d{9}$/;
		var tel = $.trim($("#tel").val());
		var tuCode = $.trim($('#text').val()); //图形验证码
		if (tuCode == "" || tuCode == null) {
			$(".tel-msg").html("请输入验证码！");
			return;
		};
		if (!tel) {
			$(".tel-msg").html("对不起，账号不能为空");
			return;
		} else if (!reg.test($("#tel").val())) {
			$(".tel-msg").text('请输入正确的手机号或邮箱地址');
			return;
		} else {
			$(".tel-msg").text('');
			var data = {
				"phone": tel,
				"imgCode": tuCode,
				"type": 4
			};
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/code/sms', //请求的路径
				data: JSON.stringify(data),
				contentType: "application/json",
				error: function error(_error2) {
					//请求失败的回调方法 
					alert('网络加载失败，请稍候再试!');
				},
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				success: function success(result) {
					if (result.code == 200) {
						$("#btn-code").attr("disabled", true);
						if (flag == true) {
							flag = false;
							timeone();
						} else {
							$(".tel-msg").html(result.msg);
						}
					} else {
						$(".tel-msg").html(result.msg);
					}
				}
			});
		}
	});
	document.querySelector('.qq').href = host + "/qqLogin";
	document.querySelector('.weixin').href = host + "/weixinLogin";
	//60秒倒计时
	function timeone() {
		$('#btn-code').html("(" + time + ")后重新发送");
		timer = setInterval(function () {
			time--;
			if (time == 0) {
				$("#btn-code").attr("disabled", false);
				$("#btn-code").html("点击获取验证码");
				time = 60;
				flag = true;
				clearInterval(timer);
			} else {
				$('#btn-code').html("(" + time + ")后重新发送");
			}
		}, 1000);
	};
	$(document).keydown(function (event) {
		if (event.keyCode == 13) {
			$('#btn').triggerHandler('click');
		}
	});
	$('#change_pic').click(function () {
		change_pic();
	});
	function change_pic() {
		var img = document.querySelector('.text_pic');
		var tel = $.trim($("#tel").val()); //账号
		if (img) {
			img.src = host + "/api/code/img?phone=" + tel;
		}
	};
});